﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataControl;
using System.Data;
using ATW.MT.CommonClass;
using System.Text;

namespace ATW.MT.BUS
{
    
    public class bus_T_Respone
    {
        DataAccess da = null;

        public bus_T_Respone()
        {
            da = new DataAccess();
        }

        #region 方法
        /// <summary>
        /// 报价分页查询
        /// </summary>
        /// <param name="title">标题</param>
        /// <param name="status">状态 0默认（未审核） 1正常（审核通过） 2删除 3审核未通过</param>
        /// <param name="pstart">发布时间开始</param>
        /// <param name="pend">发布时间结束</param>
        /// <param name="fstart">截止时间开始</param>
        /// <param name="fend">截至时间结束</param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public DataTable ResponsePagerQuery(string title, string name, string status, string pstart, string pend, int page, int pagesize)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append(@"select t1.Id,t1.RequestID,t2.title,t1.ResponseID,
                        (case 
		                when t1.rowstatus=0 then '未报价项目'
		                when t1.rowstatus=1 then '已报价项目'
		                when t1.rowstatus=2 then '已删除项目'
		                when t1.rowstatus=3 then '已收到合同'
		                when t1.rowstatus=4 then '执行中合同' 
		                when t1.rowstatus=5 then '已完成合同' 
		                when t1.rowstatus=6 then '报价被采用'
                        when t1.rowstatus=7 then '已顺延项目'
                        when t1.rowstatus=8 then '项目被关闭'
                        when t1.rowstatus=9 then '转人工干预'
		                end) as rowstatus,t1.create_time,u.tauName,t1.tauLoginId
                        from T_Response t1 
                        inner join T_Request t2 on t1.RequestID=t2.RequestID
                        left join T_AppUser u on t1.tauLoginId=u.tauLoginId
                        where 1=1");
            if (!String.IsNullOrEmpty(title))
            {
                sql.Append(" and t2.title like '%" + title + "%'");
            }
            if (!String.IsNullOrEmpty(name))
            {
                sql.Append(" and u.tauName like '%" + name + "%'");
            }

            if (!String.IsNullOrEmpty(status))
            {
                sql.Append(" and t1.rowstatus ='" + status + "'");
            }
            if (!String.IsNullOrEmpty(pstart))
            {
                sql.Append(" and t1.create_time>cast('" + pstart + " 00:00:00' as datetime)");
            }
            if (!String.IsNullOrEmpty(pend))
            {
                sql.Append(" and t1.create_time<cast('" + pend + " 23:59:59' as datetime)");
            }

            return da.PageSize(sql.ToString(), page, pagesize, "ResponseID", "create_time desc");
        }
        /// <summary>
        /// 用户报价分页查询
        /// </summary>
        /// <param name="tauloginID"></param>
        /// <param name="title"></param>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <param name="pstart"></param>
        /// <param name="pend"></param>
        /// <param name="page"></param>
        /// <param name="pagesize"></param>
        /// <returns></returns>
        public DataTable UserResponsePagerQuery(string tauloginID, string title, string status, string pstart,
                                                string pend, int page, int pagesize)
        {
            StringBuilder sql = new StringBuilder();
            sql.AppendFormat(@"select t1.Id,t1.RequestID,t2.title,t1.ResponseID,
                        (case 
		                when t1.rowstatus=0 then '未报价项目'
		                when t1.rowstatus=1 then '已报价项目'
		                when t1.rowstatus=2 then '已删除项目'
		                when t1.rowstatus=3 then '已收到合同'
		                when t1.rowstatus=4 then '执行中合同' 
		                when t1.rowstatus=5 then '已完成合同' 
		                when t1.rowstatus=6 then '报价被采用'
                        when t1.rowstatus=7 then '时间被推迟'
                        when t1.rowstatus=8 then '项目被关闭'
                        when t1.rowstatus=9 then '转人工干预'
		                end) as rowstatus,t1.create_time,u.tauName,t1.tauLoginId
                        from T_Response t1 
                        inner join T_Request t2 on t1.RequestID=t2.RequestID
                        left join T_AppUser u on t1.tauLoginId=u.tauLoginId
                        where 1=1 and t1.tauloginid='{0}' and t1.rowstatus<>'0' and t1.rowstatus<>'2' ", tauloginID);
            if (!String.IsNullOrEmpty(title))
            {
                sql.Append(" and t2.title like '%" + title + "%'");
            }
            if (!String.IsNullOrEmpty(status))
            {
                sql.Append(" and t1.rowstatus ='" + status + "'");
            }
            if (!String.IsNullOrEmpty(pstart))
            {
                sql.Append(" and t1.create_time>cast('" + pstart + " 00:00:00' as datetime)");
            }
            if (!String.IsNullOrEmpty(pend))
            {
                sql.Append(" and t1.create_time<cast('" + pend + " 23:59:59' as datetime)");
            }
            return da.PageSize(sql.ToString(), page, pagesize, "RequestID", "create_time desc");
        }
        /// <summary>
        /// 报价详情
        /// </summary>
        /// <param name="tdiid"></param>
        /// <returns></returns>
        public DataTable ResponseDetail(string strResponseId)
        {
            string strUrl = common.GetUrl();

            Dictionary<string, object> Parm = new Dictionary<string, object>();
            Parm.Add("@LanId", "zh");
            Parm.Add("@ResponseId", strResponseId);
            Parm.Add("@strUrl", strUrl);

            return da.GetDataTableBySPName("SP_SelectResponseDetail", Parm);
        }

        public DataTable getDataTable(string spName, Dictionary<string, object> param)
        {
            return da.GetDataTableBySPName(spName, param);
        }
        #endregion
    }

    
}